/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.database.metamodel;

import com.jb.web.bean.event.UIEvent;
import com.jb.web.context.WebContext;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;


public class Table extends BaseDatabaseEntity {
    String filter = "%";
    String refrenceGeneration;
    String selfReferencingColName;
    String tableCat;
    String tableRemarks;
    String tableSchema;
    String tableType;
    String typeCatalog;
    String typeName;
    String typeSchema;

    ArrayList list = new ArrayList();

    public void onSelectColumn(WebContext pCtx, UIEvent event) {
      
        List columns = getColumns();
        selectAll(false);
        for(int i=0;i<columns.size();i++) {
            Column column = (Column)columns.get(i);
            if("on".equals(event.getParameter(column.getName())))
            {
                select(column.getName(), true);
                System.err.println(column.getName());
            }
        }
    }
    
    public void select(String columnName, boolean select) {
        Column column = (Column)this.getChildByTypeAndName(Column.class, columnName);
        if(column!=null)
            column.setSelected(select);
    }
    
    public void selectAll(boolean select) {
        List list = getColumns();
        for(int i=0;i<list.size();i++) {
            Column column = (Column) list.get(i);
            column.setSelected(select);
        }
    }
    
    public List getColumns() {
        List list = this.getChildrenByType(Column.class);

        if ((list.size() == 0) || !"%".equals(filter)) {
            if (list.size() == 0) {
                list.clear();
            }

            filter = "%";
            populate(filter, "Column");
        }

        return this.getChildrenByType(Column.class);
    }

    public List getColumns(String filter) {
        List list = this.getChildrenByType(Column.class);

        if ((list.size() == 0) || !filter.equals(this.filter)) {
            if (list.size() == 0) {
                list.clear();
            }

            this.filter = filter;
            populate(filter, "Column");
        }

        populate(filter, "Column");

        return this.getChildrenByType(Column.class);
    }

    public List getIndexice(String filter) {
        populate(filter, "Index");

        return this.getChildrenByType(Index.class);
    }

    public List getPrimaryKeys(String filter) {
        populate(filter, "PrimaryKey");

        return this.getChildrenByType(com.jb.database.metamodel.PrimaryKey.class);
    }

    public void setRefrenceGeneration(String refrenceGeneration) {
        this.refrenceGeneration = refrenceGeneration;
    }

    public String getRefrenceGeneration() {
        return refrenceGeneration;
    }

    public void setSelfReferencingColName(String selfReferencingColName) {
        this.selfReferencingColName = selfReferencingColName;
    }

    public String getSelfReferencingColName() {
        return selfReferencingColName;
    }

    public void setTableCat(String tableCat) {
        this.tableCat = tableCat;
    }

    public String getTableCat() {
        return tableCat;
    }

    public void setTableRemarks(String tableRemarks) {
        this.tableRemarks = tableRemarks;
    }

    public String getTableRemarks() {
        return tableRemarks;
    }

    public void setTableSchema(String tableSchema) {
        this.tableSchema = tableSchema;
    }

    public String getTableSchema() {
        return tableSchema;
    }

    public void setTableType(String tableType) {
        this.tableType = tableType;
    }

    public String getTableType() {
        return tableType;
    }

    public void setTypeCatalog(String typeCatalog) {
        this.typeCatalog = typeCatalog;
    }

    public String getTypeCatalog() {
        return typeCatalog;
    }

    public void setTypeName(String typeName) {
        this.typeName = typeName;
    }

    public String getTypeName() {
        return typeName;
    }

    public void setTypeSchema(String typeSchema) {
        this.typeSchema = typeSchema;
    }

    public String getTypeSchema() {
        return typeSchema;
    }

    public void assign(ResultSet crs) throws SQLException {
        setTableCat(crs.getString(1));
        setTableSchema(crs.getString(2));
        setName(crs.getString(3));
        setTableType(crs.getString(4));
        setTableRemarks(crs.getString(5));
    }

    private void populate(String filter, String type) {
        Connection conn = null;

        try {
            conn = com.jb.framework.DataSourceManager.getConnection(this);
            this.populate(conn, filter, type);
            conn.commit();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

            com.jb.framework.DataSourceManager.releaseConnection(this, conn);
        }
    }
    public void populate(Connection conn, String filter, String type) {
        ResultSet crs = null;
        try { 
            if (type.equalsIgnoreCase("Column")) {
                crs = conn.getMetaData().getColumns(tableCat, tableSchema, getName(), filter);

                while (crs.next()) {
                    Column col = new Column();
                    col.assign(crs);
                    addChild(col);
                }

                crs.close();
                crs = null;
            }

            if (type.equalsIgnoreCase("PrimaryKey")) {
                crs = conn.getMetaData().getPrimaryKeys(tableCat, tableSchema, getName());

                while (crs.next()) {
                    PrimaryKey pk = new PrimaryKey();
                    pk.assign(crs);
                    addChild(pk);
                    System.out.println(pk);
                }

                crs.close();
                crs = null;
            }

            if (type.equalsIgnoreCase("PrimaryKey")) {
                crs = conn.getMetaData().getIndexInfo(tableCat, tableSchema, getName(), false, false);
                System.out.println("Indexes Key");

                while (crs.next()) {
                    Index index = new Index();
                    index.assign(crs);
                    addChild(index);
                    System.out.println(index);
                }

                crs.close();
                crs = null;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (crs != null) {
                try {
                    crs.close();
                } catch (Exception e) {
                }
            }

        }
    }

}

//
